<div class="layuimini-main">
    <div class="layui-form layuimini-form" lay-filter="updateForm">

        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="id" id="id">
                <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">开始时间</label>
            <div class="layui-input-block">
                <input type="text" name="startTime" id="startTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">结束时间</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" id="endTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label required">班级</label>
            <div class="layui-input-block">
                <input type="hidden" name="clazzId" id="clazzId" lay-verify="required"  class="layui-input">
                <input type="text" name="clazzName" readonly id="clazzName" lay-verify="required" lay-reqtext="班级不能为空" placeholder="请选择班级" value="" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table','axios','laydate','selectOrg'], function () {
        let form = layui.form,
            layer = layui.layer,
            axios = layui.axios,
            laydate = layui.laydate,
            selectOrg = layui.selectOrg,
            $ = layui.$;


        //日期
        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();


        selectOrg.render({
            elem:'#clazzName',
            selected:[],
            checked:true,
            done:function (data) {
                let nameArr = data.map(item=>item.name);
                $('#clazzName').val(nameArr.join(','));
                let idArr = data.map(item=>item.id);
                $('#clazzId').val(idArr);
            }
        });

        // 当前弹出层，防止ID被覆盖
        let parentIndex = layer.index;



        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let param = data.field;
            if(data.field.clazzId){
                param.clazzIds =data.field.clazzId.split(",");
            }
            axios.post('selection/update',param).then(function(response){
                if(response.code == 200){
                    layer.msg(response.msg);
                    window.reload();
                }
                layer.close(parentIndex);
            }).catch(function (error) {
                layer.msg(error);
            });
            return false;
        });

    });
</script>